Spell = CLASS('Spell')

function Spell:ctor( name, target )
	self.name = name
	self.target = target 
end

function Spell:getTarget()
	-- body
	local t = self.target
	if self.target == 'other' then
		t = PVPOther()
	end
	return t
end

function Spell:cast()
	--print(self.name, self.target, IsSpellInRange(self.name, self.target))

	local t = self:getTarget()
	local usable, nomana = IsUsableSpell(self.name)
	local start, duration, enabled = GetSpellCooldown(self.name)
	-- print(usable, start)
	if not usable or start ~= 0 then
		return false
	end
	if  t and (not UnitExists(t) or UnitIsDead(t) or not self:checkDistance())then
		return false
	end


	if self.enable == false then
		return false
	end

	self:beforeJudge()
	if not self:canCast() then
		return false
	end
	self:beforeCast()
	CastSpellByName(self.name, t)
	self:afterCast()
	return true
end
function Spell:beforeCast()
end
function Spell:afterCast()
end

function Spell:beforeJudge()
end

----------------------------virtual functions
function Spell:canCast( )
	return true
end

function Spell:checkDistance()
	--print(self.name, self.target, IsSpellInRange(self.name, self.target))
	return IsSpellInRange(self.name, self:getTarget()) ~= 0
end


-----------------------------------------

MovableSpell = CLASS(Spell)
function MovableSpell:canMovingCasting( )
end


-----------------------------------------
function le(arg1, arg2)
	return arg1 <= arg2
end

function ge( arg1, arg2 )
	return arg1 >= arg2
end

TargetDebuff = CLASS(Spell)

function TargetDebuff:ctor( name, target, debuff, compareFunc, time )
-- print( name, debuff, compareFunc, time )
	self.debuff = debuff or name
	self.time = time
	self.compareFunc = compareFunc
end

function TargetDebuff:canCast( )
	local t = getDebuffRemainTime( self.target, self.debuff )
	return self.compareFunc(t, self.time)
end

-----------------------------------------
PlayerBuff = CLASS(TargetDebuff)

function PlayerBuff:canCast( )
	local t = getBuffRemianTime( 'player', self.debuff )
	return self.compareFunc(t, self.time)
end


print('Spell.lua load ...')
